import { login,  getInfo } from '@/api/manager'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import router from '../../router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  role:'',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLE: (state, role) => {
    state.role = role
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_ROLE', data.role.value)
        localStorage.setItem('role',data.role.value)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo({token:state.token}).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        localStorage.setItem('role',data.role.value)
        localStorage.setItem('id',data.id)
        const { name, avatar } = data
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        let routes=[]
        router.options.routes=JSON.parse(JSON.stringify(router.options.routesTemp))
        router.options.routes.map((res,index)=>{
          if(index<4){
            routes.push(res)
            return
          }
          if(data.role.value=='admin'){
            let children=[]
            res.children&&res.children.map(r=>{
              if(r.meta.admin&&!r.hidden){
                children.push(r)
              }
            })
            if(children.length){
              res.children=children
            }else{
              res=null
            }
          }else{
            let children=[]
            res.children&&res.children.map(r=>{
              if(!r.meta.admin&&!r.hidden){
                children.push(r)
              }
            })
            if(children.length){
              res.children=children
            }else{
              res=null
            }
          }
          if(res){
            routes.push(res)
          }
        })
        router.options.routes=routes
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      localStorage.removeItem('role')
      removeToken()
      resetRouter()
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

